<?php

namespace App\Presenters;

use App\Models\Topic;

class TopicPresenter
{
    public function getLastReply(Topic $topic)
    {
        if ($topic->lastComment_id == 0)
        {
            return '';
        }
        return sprintf(
            '<small>%s</small><small>%s</small>', 
            $topic->lastComment->user->name,
            $topic->lastComment->created_at->format('Y-m-d H:i')
        );
    }

    public function renderReport(Topic $topic)
    {
        $html = [];
        $html[] = sprintf('<div><strong>出击时间：</strong>%s</div>', $topic->action_time ?? '未知');
        $html[] = sprintf(
            '<div><strong>出击地点：</strong>%s-%s-%s，%s</div>',
            $topic->action_province, $topic->action_city, $topic->action_district, $topic->action_location
        );

        if ($topic->tags->isNotEmpty()) {
            $html[] = sprintf(
                '<div><strong>服务标签：</strong>%s</div>',
                $topic->tags->pluck('name')->map(function ($item) {
                    return sprintf('<button class="primary">%s</button>', $item);
                })->implode('')
            );
        }

        return implode("", $html);
    }

    public function renderLastOperationLog(Topic $topic)
    {
        $operationLog = $topic->lastOperationLog;
        $msg = sprintf('<small>帖子最后由 %s 于 %s 执行 %s 操作</small>', $operationLog->user->username, $operationLog->created_at->format('Y-m-d H:i'), $operationLog->type_name);
        return $msg;
    }
}